package top.xianggg.xservers.common.base.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 非资源类标准返回格式
 * @author Jr.November
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private int code;
    private T data;
    private String msg;

    Result(ResultCode resultCode){
        this.code= resultCode.value;
        this.msg = resultCode.defaultMsg;
    }
    Result(ResultCode resultCode, T data){
        this(resultCode);
        this.data = data;
    }

    public static Result success(){
        return new Result(ResultCode.success);
    }

    public static <T> Result<T> success(T data){
        return new Result<>(ResultCode.success, data);
    }

    public static Result error(ResultCode code){
        return error(code,code.defaultMsg);
    }
    public static Result error(ResultCode code, String msg){
        return new Result<>(code.value,null, msg);
    }

    /**
     * 错误代码定义
     * 400：
     */
    @AllArgsConstructor
    public enum ResultCode {
        /**
         * 成功
         */
        success(0,"success"),
        /**
         * 未知错误
         */
        unknown(10000,"未知错误"),
        /**
         * 自定义错误, 前端按消息处理
         */
        costum(1,"自定义错误"),
        loginFail(2,"登录失败"),
        /**
         * 未授权
         */
        unauthorized(403,"未授权访问"),
        internal_server_error(500, "服务器内部错误");;

        public int value;
        public String defaultMsg;

    }
}
